package com.zzu.view.fragment;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.zzu.R;
import com.zzu.model.bean.DLS;
import com.zzu.model.bean.JsonBean;
import com.zzu.presenter.impl.PackagePresenterImpl;
import com.zzu.utils.AppStorage;
import com.zzu.utils.Constants;
import com.zzu.utils.GetJsonDataUtil;
import com.zzu.adapter.ThreeAdapter;
import com.zzu.view.view.PackageQueryView;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;


/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-07 23:35
 * 揽收Fragment
 */
public class FragmentThree extends BaseFragment {

    private final String TAG = FragmentThree.class.getName();

    private ArrayList<JsonBean> options1Items = new ArrayList<>(); //省
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();//市
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();//区

    //显示地址信息
    TextView tv_three_address;
    //进行地址选择
    TextView tv_three_address_select;
    //当没有快递信息是的显示布局
    LinearLayout ll_item_empty;
    //显示揽收信息的RecyclerView
    RecyclerView three_recyclerview;
    //当前选择的区或县名
    private String twn = "中原区";


    @Override
    public View initView() {
        Log.e(TAG, "揽收任务视图被初始化了");

        View view = View.inflate(mContext, R.layout.fragment_three, null);

        tv_three_address = view.findViewById(R.id.tv_three_address);
        tv_three_address_select = view.findViewById(R.id.tv_three_address_select);
        ll_item_empty = view.findViewById(R.id.ll_item_empty);
        //没有包裹时显示的文字
        TextView tv_item_empty = view.findViewById(R.id.tv_item_empty);
        tv_item_empty.setText(Constants.FRAGMENT_EMPTY_THREE);
        three_recyclerview = view.findViewById(R.id.three_recyclerview);

        //为右上角地址选择添加监听事件
        initListener();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        //显示揽收的数据，初使显示中原区信息
        showData(twn);
    }

    private void showData(String twn) {
        //异步获取到所有数据

        new PackagePresenterImpl(mContext,new PackageQueryView<List<DLS>>() {
            @Override
            public void getDates(List<DLS> datas) {
                Log.i(TAG,"获取区为"+twn+"的数据:"+datas);
                //查询到数据
                //把当没有数据显示的布局-隐藏
                ll_item_empty.setVisibility(View.GONE);
                ThreeAdapter adapter = new ThreeAdapter(mContext,datas);
                three_recyclerview.setAdapter(adapter);
                //设置布局管理器
                three_recyclerview.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
            }

            @Override
            public void getNoDate() {
                //没有揽收的数据
                //显示数据为空的布局
                ShowEmptyFragment();
            }
        }).getDLSTask(twn);

    }

    //数据为空时显示的布局
    private void ShowEmptyFragment() {
//        three_recyclerview.setVisibility(View.INVISIBLE);
        ll_item_empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void initData() {
        super.initData();
        Log.e(TAG, "揽收任务数据被初始化了");

    }

    private void initListener() {
        //
        tv_three_address_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 弹出选择器（省市区三级联动）
                showPickerView();

            }
        });

    }

    // 弹出选择器（省市区三级联动）
    private void showPickerView() {

        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                JsonBean jsonBean = AppStorage.options1Items.get(options1);
                //返回的分别是三个级别的选中位置,更改地址信息
                tv_three_address.setText(AppStorage.options1Items.get(options1).getPickerViewText() + "  "
                        + AppStorage.options2Items.get(options1).get(options2) + "  "
                        + AppStorage.options3Items.get(options1).get(options2).get(options3));
                Log.i(TAG,"切换到"+AppStorage.options3Items.get(options1).get(options2).get(options3)+"的城市");
                //显示揽收的数据
                twn = AppStorage.options3Items.get(options1).get(options2).get(options3);
                showData(twn);
            }
        })
                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
//        pvOptions.setPicker(options1Items);//一级选择器
//        pvOptions.setPicker(options1Items, options2Items);//二级选择器
        pvOptions.setPicker(AppStorage.options1Items, AppStorage.options2Items, AppStorage.options3Items);//三级选择器
        pvOptions.show();
    }

}
